package com.sylli.oeuf.server.db.sql;

import java.io.*;


import com.sylli.oeuf.server.db.DatabaseProviderBundle;
import com.sylli.oeuf.server.exception.BasedException;
import com.sylli.oeuf.server.object.transferable.AccountTO;
import com.sylli.oeuf.server.object.transferable.CharacterGeneralInfoTO;
import com.sylli.oeuf.server.object.transferable.CharacterPostDataTO;
import com.sylli.oeuf.server.object.transferable.MapBlockTO;
import com.sylli.oeuf.server.object.transferable.MapMonsterTO;
import com.sylli.oeuf.server.object.transferable.MonsterTO;
import com.sylli.oeuf.server.object.transferable.StatTO;

public class Generator {

	private DatabaseProviderBundle providers = DatabaseProviderBundle.getInstance();
	
	public void execute() throws BasedException 
	{
		executeMapBlock();
		System.out.println("Completed MapBlock.");
		executeMonster();
		System.out.println("Completed Monster.");
		executeMapMonster();
		System.out.println("Completed MapMonster.");
		executeAccount();
		System.out.println("Completed Account.");
		executeCharGenInfo() ;
		System.out.println("Completed CharGenInfo.");
		executeCharPostData();
		System.out.println("Completed PostData.");
	}
	public void executeCharGenInfo() throws BasedException 
	{
		CharacterGeneralInfoTO record1  = new CharacterGeneralInfoTO();
		StatTO  stat1 = new StatTO(8,8,8,8,8,8,8,8,8,8);
		record1.setAccountId(1); record1.setClazzId(6) ;record1.setLevel(10); record1.setMaxExp(2000); record1.setMaxHp(200); record1.setMaxMp(100); record1.setName("haloowing"); record1.setStat(stat1);
		
		CharacterGeneralInfoTO record2  = new CharacterGeneralInfoTO();
		StatTO  stat2 = new StatTO(12,5,5,10,7,5,5,14,5,5);
		record2.setAccountId(2); record2.setClazzId(1) ;record2.setLevel(12); record2.setMaxExp(4000); record2.setMaxHp(220); record2.setMaxMp(120); record2.setName("freeuxer"); record2.setStat(stat2); 
		
		CharacterGeneralInfoTO record3  = new CharacterGeneralInfoTO();
		StatTO  stat3 = new StatTO(18,8,12,8,16,5,18,5,20,5);
		record3.setAccountId(3); record3.setClazzId(2) ;record3.setLevel(20); record3.setMaxExp(40000); record3.setMaxHp(400); record3.setMaxMp(250); record3.setName("meteo"); record3.setStat(stat3); 	
		
		
		CharacterGeneralInfoTO record4  = new CharacterGeneralInfoTO();
		StatTO  stat4 = new StatTO(18,8,12,8,16,5,18,5,20,5);
		record4.setAccountId(3); record4.setClazzId(3) ;record4.setLevel(20); record4.setMaxExp(40000); record4.setMaxHp(400); record4.setMaxMp(250); record4.setName("ey"); record4.setStat(stat4); 	
		
		
		CharacterGeneralInfoTO record5  = new CharacterGeneralInfoTO();
		StatTO  stat5 = new StatTO(18,8,12,8,16,5,18,5,20,5);
		record5.setAccountId(3); record5.setClazzId(5) ;record5.setLevel(20); record5.setMaxExp(40000); record5.setMaxHp(400); record5.setMaxMp(250); record5.setName("gblue"); record5.setStat(stat5); 	
		
		CharacterGeneralInfoTO record6  = new CharacterGeneralInfoTO();
		StatTO  stat6 = new StatTO(18,8,12,8,16,5,18,5,20,5);
		record6.setAccountId(3); record6.setClazzId(5) ;record6.setLevel(20); record6.setMaxExp(40000); record6.setMaxHp(400); record6.setMaxMp(250); record6.setName("dinosao"); record6.setStat(stat6); 	
		
		
		providers.charGenInfo.create(record1);
		providers.charGenInfo.create(record2);
		providers.charGenInfo.create(record3);	
		providers.charGenInfo.create(record4);	
		providers.charGenInfo.create(record5);	
		providers.charGenInfo.create(record6);	
	}
	public void executeCharPostData() throws BasedException
	{
		CharacterPostDataTO record1 = new CharacterPostDataTO();
		CharacterPostDataTO record2 = new CharacterPostDataTO();
		CharacterPostDataTO record3 = new CharacterPostDataTO();
		CharacterPostDataTO record4 = new CharacterPostDataTO();
		CharacterPostDataTO record5 = new CharacterPostDataTO();
		CharacterPostDataTO record6 = new CharacterPostDataTO();
		record1.setCharId(1); record1.setCurrentExp(100); record1.setCurrentHp(200); record1.setCurrentMap(0); record1.setCurrentMp(100); record1.setCurrentPos(4004); record1.setTotalTurn(2600); record1.setTurnLeft(100);
		record2.setCharId(2);record2.setCurrentExp(2000); record2.setCurrentHp(300); record2.setCurrentMap(0); record2.setCurrentMp(150); record2.setCurrentPos(6010); record2.setTotalTurn(4000); record2.setTurnLeft(127);
		record3.setCharId(3);record3.setCurrentExp(4605); record3.setCurrentHp(700); record3.setCurrentMap(0); record3.setCurrentMp(350); record3.setCurrentPos(12005); record3.setTotalTurn(20000); record3.setTurnLeft(234);
		record4.setCharId(4);record4.setCurrentExp(4605); record4.setCurrentHp(700); record4.setCurrentMap(0); record4.setCurrentMp(350); record4.setCurrentPos(12005); record4.setTotalTurn(20000); record4.setTurnLeft(234);
		record5.setCharId(5);record5.setCurrentExp(4605); record5.setCurrentHp(700); record5.setCurrentMap(0); record5.setCurrentMp(350); record5.setCurrentPos(12005); record5.setTotalTurn(20000); record5.setTurnLeft(234);
		record6.setCharId(6);record6.setCurrentExp(4605); record6.setCurrentHp(700); record6.setCurrentMap(0); record6.setCurrentMp(350); record6.setCurrentPos(12005); record6.setTotalTurn(20000); record6.setTurnLeft(234);
		providers.charPostData.create(record1);
		providers.charPostData.create(record2);
		providers.charPostData.create(record3);
		providers.charPostData.create(record4);
		providers.charPostData.create(record5);
		providers.charPostData.create(record6);
	}
	public void executeAccount() throws BasedException
	{
		AccountTO record1 = new AccountTO();
		AccountTO record2 = new AccountTO();
		AccountTO record3 = new AccountTO();
		record1.setEmail("khem@hotmail.com"); record1.setFlag(0); record1.setPassword("khem"); record1.setUsername("haloowing");
		record2.setEmail("mah@hotmail.com"); record2.setFlag(0); record2.setPassword("mah"); record2.setUsername("freeuxer");
		record3.setEmail("gap@hotmail.com"); record3.setFlag(0); record3.setPassword("gap"); record3.setUsername("meteo");
		
		providers.account.create(record1);
		providers.account.create(record2);
		providers.account.create(record3);	
	}
	public void executeMapBlock() throws BasedException
	{ 
		
		try
		{
		MapBlockTO record = new MapBlockTO ();
		for(int k=0 ; k < 5 ; k++)
		{
			int flag ; 
			String param ;
			int terrain;
			
			File testFile = new File("WEB-INF\\test\\com\\sylli\\oeuf\\server\\db\\sql\\mapflag" + k + ".txt");
			File testFile2 = new File("WEB-INF\\test\\com\\sylli\\oeuf\\server\\db\\sql\\mapterrain" + k + ".txt");
			StringBuilder contents = new StringBuilder();    
			StringBuilder contents2 = new StringBuilder();
			try
			{
				if (testFile.exists() == true)
				{
					BufferedReader input =  new BufferedReader(new FileReader(testFile)); 
					try 
					{
					     
						
					    String line = null; 
					      
					    while (( line = input.readLine()) != null)
					    {
					       contents.append(line);
					    }  
					}  
					finally 
					{
					        input.close();
					}
				}
				
			}
			catch(Exception e)
			{
				 e.printStackTrace();
			}
			try
			{
				if(testFile2.exists() == true)
				{
					BufferedReader input2 =  new BufferedReader(new FileReader(testFile2)); 
					try 
					{
					     
						
					    String line = null; 
					      
					    while (( line = input2.readLine()) != null)
					    {
					       contents2.append(line);
					    }  
					}  
					finally 
					{
					        input2.close();
					}
				}
			}
			catch(Exception e)
			{
				 e.printStackTrace();
			}
			for (int i = 0 ; i<20; i++)
			{
				for(int j = 0 ; j< 20 ; j++)
				{
					if(contents.length() != 0)
					{	
						String temp = contents.substring(0, contents.indexOf(" "));
						String temp2 = temp.substring(0,contents.indexOf(":") ); 
						String temp3 = temp.substring(temp.indexOf(":") +1, temp.length() - temp.indexOf(":") +1 );
						flag = Integer.parseInt(temp2);
						param = temp3;
						contents.delete(0, contents.indexOf(" ") +1);
					}
					else
					{
					  flag = 0 ;
					  param ="";
					}
					if(contents2.length() != 0)
					{
						
						String temp = contents2.substring(0, contents2.indexOf(" "));
						terrain = Integer.parseInt(temp);
						contents2.delete(0, contents2.indexOf(" ") +1);
						
					}
					else
					{
						terrain = 0 ;
					}
					/*record.setBlockId(i * 1000 +j); record.setMapId(k); record.setFlag(1);record.setParam(""); record.setTerrainType(0);
					providers.mapBlock.create(record);*/
					record.setBlockId(j * 1000 +i); record.setMapId(k); record.setFlag(flag);record.setParam(param);record.setTerrainType(terrain);
					providers.mapBlock.create(record);
				}
			}
		}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	public void executeMonster() throws BasedException
	{
		MonsterTO record1 = new MonsterTO();
		MonsterTO record2 = new MonsterTO();
		MonsterTO record3 = new MonsterTO();
		record1.setExp(30); record1.setFlag(0); record1.setLevel(1); record1.setName("poring"); record1.setStatHp(30); record1.setStatMp(20); record1.setStat(new StatTO(30, 13, 2, 2, 2, 2, 2, 22, 2, 2));
		record2.setExp(50); record2.setFlag(0); record2.setLevel(2); record2.setName("lunatic"); record2.setStatHp(40); record2.setStatMp(25); record2.setStat(new StatTO(30, 13, 2, 2, 2, 2, 2, 22, 2, 2));
		record3.setExp(80); record3.setFlag(0); record3.setLevel(3); record3.setName("goblin"); record3.setStatHp(60); record3.setStatMp(40); record3.setStat(new StatTO(30, 13, 2, 2, 2, 2, 2, 22, 2, 2));
		
		providers.monster.create(record1);
		providers.monster.create(record2);
		providers.monster.create(record3);
	}
	public void executeMapMonster() throws BasedException
	{
		try
		{
		MapMonsterTO record = new MapMonsterTO();
		for(int k=0 ; k < 5 ; k++)
		{
			int flag ; 
			String param ;
			int terrain;
			File testFile = new File("WEB-INF\\test\\com\\sylli\\oeuf\\server\\db\\sql\\mapflag" + k + ".txt");
			StringBuilder contents = new StringBuilder();    
			try
			{
				if(testFile.exists()== true)
				{
					BufferedReader input =  new BufferedReader(new FileReader(testFile)); 
					try 
					{
					     
						
					    String line = null; 
					      
					    while (( line = input.readLine()) != null)
					    {
					       contents.append(line);
					    }  
					}  
					finally 
					{
					        input.close();
					}
				}
				
			}
			catch(Exception e)
			{
				 e.printStackTrace();
			}
			for (int i = 0 ; i<20; i++)
			{
				for(int j = 0 ; j< 20 ; j++)
				{
					if(contents.length() != 0)
					{	
						String temp = contents.substring(0, contents.indexOf(" "));
						String temp2 = temp.substring(0,contents.indexOf(":") ); 
						String temp3 = temp.substring(temp.indexOf(":") +1, temp.length() - temp.indexOf(":") +1 );
						flag = Integer.parseInt(temp2);
						param = temp3;
						contents.delete(0, contents.indexOf(" ") +1);
					}
					else
					{
					  flag = 0 ;
					  param ="";
					}
					
					if(flag == 1)
					{
						int percentOld = 0;
						for(int l = 1 ; l< 4 ; l++ )
						{
							int percent = (int)(Math.random() * 100);
							if(percentOld + percent > 100)
							{
								percent = 100 - percentOld ; 
							}
							if(l == 3)
							{
								percent = 100 - percentOld ; 
							}
							percentOld += percent ; 
							
							record.setBlockId(j * 1000 + i); record.setMapId(k); record.setSpawnPerc(percent); record.setMonsterId(l); 
							providers.mapMonster.create(record);
						}
					}
				}
			}	
		}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Generator g = new Generator();
		
		try {
			g.execute();
		} catch (BasedException e) {
			e.printStackTrace();
		}
	}

}
